<?php
class Controller extends Item{

    public $layout;
    public $pageTitle;
    public $style;
    public $script;

    public function __construct(){
        parent::__construct();
    }



    /**
     * 获取layout框架
     */
    public function getLayout(){
        return empty($this->layout)?'default':$this->layout;
    }

    /**
     * 设置layout框架
     * @param unknown_type $layout
     */
    public function setLayout($layout='default'){
        $this->layout = $layout;
    }

    public function getTitle(){
        $config = $this->config;
        return empty($this->pageTitle)?$config['appName']:$this->pageTitle;
    }

    public function setTitle($pageTitle){
        $this->pageTitle = $pageTitle;
    }

    public function getScript(){
        if(count($this->script)==0){
            return null;
        }
        foreach($this->script as $script){
            return $this->loadScript($script);
        }
    }

    public function setScript($scripts=array()){
        if(count($scripts)==0) return null;
        $this->script = $scripts;
    }

    public function loadScript($script){
        $config = $this->config;
        $scriptName = $config['base']['script'].nameToPath($script,'.js');
        ob_start();
        ob_implicit_flush(false);
        echo "<script type='text/javascript' src='{$scriptName}'></script>";
        return ob_get_clean();
    }

    public function loadScripts($scripts){
        if(is_array($scripts)&&count($scripts)>0){
            $_scrips = '';
            foreach($scripts as $script){
                $_scrips .= $this->loadScript($script)."\r\n";
            }
            return $_scrips;
        }
        return null;
    }

    public function getSytle(){
        if(count($this->style)==0){
            return null;
        }
        foreach($this->style as $style){
            return $this->loadStyle($style);
        }
    }

    public function setStyle($styles=array()){
        if(count($styles)==0) return null;
        $this->style = $styles;
    }

    public function loadStyle($style){
        $config = $this->config;
        $styleName = $config['base']['style'].nameToPath($style,'.css');
        ob_start();
        ob_implicit_flush(false);
        echo "<link rel='stylesheet' type='text/css' media='screen' href='{$styleName}' />\r\n";
        return ob_get_clean();
    }

    public function loadStyles($styles){
        if(is_array($styles)&&count($styles)>0){
            $_styles = '';
            foreach($styles as $style){
                $_styles .= $this->loadStyle($style)."\r\n";
            }
            return $_styles;
        }
        return null;
    }



    public function redirect($url){
        if(is_array($url)){
            $url = $this->createUrl($url[0]);
        }
        Header("Location: {$url}");
    }

    public function getParams(){
        return $this->params;
    }
}